<?php


namespace app\admin\model;


use think\Db;

class PayDepositModel extends BaseModel
{
    protected $name='pay_deposit';

    protected $autoWriteTimestamp = 'int';
    protected $updateTime = false;
    protected $createTime = 'add_time';

    public function doAddDate($data=[]){

        $customerInfo = CustomerInfoModel::find($data['customer_id']);
        $productInfo = TombareaInformationProductModel::where(['tomb_id'=>$data['mw_id']])->find();
        $this->startTrans();
        try {
            $pay_arr = [
                'deposit_sn' => $this->getPayNum('add_time', 'deposit_sn', 'DJ'),
                'dtype' => 1,
                'customer_id' => $data['customer_id'],
                'order_id' => $data['order_id'],
                'orders_id' => $data['orders_id'],
                'buyer_name' => $customerInfo['buyer_name'],
                'buyer_phone' => $customerInfo['buyer_phone'],
                'cart_type' => $customerInfo['cart_type'],
                'cart_num' => $customerInfo['customer_account'],
                'mw_id' => $data['mw_id'],
                'mw_num' => $data['mw_num'],
                'mw_address' => $productInfo['tomb_region'] . $productInfo['tomb_address'] . $productInfo['layer_num'] . $productInfo['tomb_bit_number'],
                'closing_price' => $data['closing_price'],
                'deposit_price' => $data['deposit_price'],
                'deposit_type' => $data['deposit_type'],
                'handle' => cmf_get_current_admin_id(),
                'status' => 0,
                'real_phone' => $data['real_phone'],
                'real_name' => $data['real_name'],
                'end_time' =>date_to_unixtime($data['end_time']),
                'pay_id' => 0,
            ];

            $info = $this->create($pay_arr);
            $other_id = $info['id'];
            $pay_ins = [
                'type' => 1,
                'order_id' => $data['order_id'],
                'pay_sn' => OrderPayLogModel::getselfPayNum('add_time', 'pay_sn', 'DJ'),// 'DJ'.date('YmdHi'),
                'tomb_id' => $data['mw_id'],
                'tomb_num' => $data['mw_num'],
                'deposit_price' => $data['deposit_price'],//定金
                'deal_price' => $data['closing_price'],//成交价格
                'total_price' => $data['deposit_price'],//定金
                'actual_amount' => $data['closing_price'],//实际结果
                'real_name' => $data['real_name'],//付款人
                'real_phone' => $data['real_phone'],//付款人
                'pay_type' => $data['deposit_type'],//付款方式
                'customer_id' => $data['customer_id'],//
                'handle' => cmf_get_current_admin_id(),//备注
                'add_time' => time(),//备注
                'status' => 0,// 付款方式
                'other_id' => $other_id,// 付款方式
            ];
            $pay_id = OrderPayLogModel::insertGetId($pay_ins);
            $this->update(['pay_id' => $pay_id], ['id' => $other_id]);
            $this->commit();
            return true;
        }catch(\Exception $e) {
            $this->error = '操作失败';
            $this->rollback();
            return false;
        }
    }

    public function DJchaishi(){
       return $this->update(['status'=>3],['status'=>1,'end_tine'=>['lt',time()]]);

    }
}